package com.small.account.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.common.basis.result.Result;
import com.common.basis.result.ResultSet;
import com.small.account.entity.AuMechanism;
import com.small.account.entity.NodeDrop;
import com.small.account.entity.vo.AuMechanismDrop;
import com.small.account.service.IAuMechanismService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 公司部门
 *
 * @author wangguochao
 * @auther W.GC
 * @date 2021-01-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("/au-mechanism")
public class AuMechanismController {


    private final IAuMechanismService iAuMechanismService;


    /**
     * 查询全部
     */
    @GetMapping()
    public Result<List<AuMechanism>> findTree() {
        List<AuMechanism> tree = iAuMechanismService.findTree();
        return ResultSet.go(tree);
    }

    /**
     * 保存-新增
     */
    @PostMapping()
    public Result<?> save(@RequestBody @Validated AuMechanism param) {
        boolean b = iAuMechanismService.save(param);
        return ResultSet.operation(b);
    }

    /**
     * 保存-修改
     */
    @PutMapping()
    public Result<?> modify(@RequestBody @Validated AuMechanism param) {
        boolean b = iAuMechanismService.updateById(param);
        return ResultSet.operation(b);
    }

    /**
     * 删除
     */
    @DeleteMapping()
    public Result<?> del(@RequestParam String id) {
        boolean b = iAuMechanismService.removeById(id);
        return ResultSet.operation(b);
    }

    /**
     * 拖动排序
     *
     * @param param 参数
     */
    @PostMapping("node-drop")
    public Result<?> nodeDrop(@RequestBody AuMechanismDrop param) {
        AuMechanism original = param.getOriginal();
        AuMechanism target = param.getTarget();
        String id = original.getId();
        String parentId = target.getParentId();
        String parentIds = target.getParentIds();
        String companyId = target.getCompanyId();
        String companyName = target.getCompanyName();
        int sortNumber = 0;
        //某某之后
        if (param.getNodeDrop() == NodeDrop.after) {
            original.setSortNumber(target.getSortNumber() + 1);
        } else if (param.getNodeDrop() == NodeDrop.before) {
            sortNumber = target.getSortNumber() - 1;
        } else if (param.getNodeDrop() == NodeDrop.inner) {
            parentId = target.getId();
            parentIds = target.getParentIds() + "," + target.getId();
        }
        LambdaUpdateWrapper<AuMechanism> update = Wrappers.lambdaUpdate();
        String ids = original.getParentIds() + "," + original.getId();
        update.likeRight(AuMechanism::getParentIds, ids)
                .setSql("parent_ids  = replace(parent_ids,'" + ids + "','" + parentIds + "," + original.getId() + "')")
                .set(AuMechanism::getCompanyId, companyId)
                .set(AuMechanism::getCompanyName, companyName);
        iAuMechanismService.update(update);
        LambdaUpdateWrapper<AuMechanism> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(AuMechanism::getId, id)
                .set(AuMechanism::getParentIds, parentIds)
                .set(AuMechanism::getParentId, parentId)
                .set(AuMechanism::getCompanyId, companyId)
                .set(AuMechanism::getCompanyName, companyName)
                .set(AuMechanism::getSortNumber, sortNumber)
                .set(AuMechanism::getSortTime, LocalDateTime.now());
        iAuMechanismService.update(updateWrapper);
        return ResultSet.success();
    }

}

